﻿using System;
using System.Windows.Forms;

namespace PengSW.DbTools
{
    public partial class SqlConnectionStringEdit : UserControl
    {
        public SqlConnectionStringEdit()
        {
            InitializeComponent();
            ConnectionString = "";
            SetOperationState();
        }

        public event System.EventHandler Changed;

        public bool ParametersIsReady
        {
            get
            {
                if (txtDataSource.Text.Trim().Length == 0) return false;
                if (txtInitialCatalog.Text.Trim().Length == 0) return false;
                if (optIntegratedSecurityFalse.Checked && txtUserID.Text.Trim().Length==0) return false;
                return true;
            }
        }

        public bool IntegratedSecurityEnable
        {
            get
            {
                return lblIntegratedSecurity.Visible;
            }
            set
            {
                lblIntegratedSecurity.Visible = value;
                optIntegratedSecurityFalse.Visible = value;
                optIntegratedSecurityTrue.Visible = value;
                if (!value) IntegratedSecurity = false;
            }
        }

        public string DataSource 
        { 
            get { return txtDataSource.Text; }
            set { txtDataSource.Text = value; }
        }
        public string InitialCatalog 
        { 
            get { return txtInitialCatalog.Text; }
            set { txtInitialCatalog.Text = value; }
        }
        public bool IntegratedSecurity 
        {
            get { return optIntegratedSecurityTrue.Checked; }
            set { optIntegratedSecurityTrue.Checked = value; optIntegratedSecurityFalse.Checked = !value; }
        }
        public string UserID 
        { 
            get { return txtUserID.Text; }
            set { txtUserID.Text = value; }
        }
        public string Password 
        {
            get { return txtPassword.Text; }
            set { txtPassword.Text = value; }
        }

        public string ConnectionString
        {
            get
            {
                m_SqlConnectionStringBuilder.DataSource = DataSource;
                m_SqlConnectionStringBuilder.InitialCatalog = InitialCatalog;
                m_SqlConnectionStringBuilder.IntegratedSecurity = IntegratedSecurity;
                m_SqlConnectionStringBuilder.UserID = UserID;
                m_SqlConnectionStringBuilder.Password = Password;
                return m_SqlConnectionStringBuilder.ConnectionString;
            }
            set
            {
                m_SqlConnectionStringBuilder.ConnectionString = value;

                m_Initing = true;
                DataSource = m_SqlConnectionStringBuilder.DataSource;
                InitialCatalog = m_SqlConnectionStringBuilder.InitialCatalog;
                IntegratedSecurity = m_SqlConnectionStringBuilder.IntegratedSecurity;
                UserID = m_SqlConnectionStringBuilder.UserID;
                Password = m_SqlConnectionStringBuilder.Password;
                m_Initing = false;

                SetOperationState();
            }
        }

        private System.Data.SqlClient.SqlConnectionStringBuilder m_SqlConnectionStringBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder();
        private bool m_Initing = false;

        private void SetOperationState()
        {
            panLoginInfo.Visible = optIntegratedSecurityFalse.Checked;
        }

        private void Parameter_Changed(object sender, EventArgs e)
        {
            if (m_Initing) return;
            SetOperationState();
            if (Changed != null) Changed(this, e);
        }
    }
}
